import React, {useEffect, useState} from 'react';
import {
    View,
    Text,
    TextInput,
    Button,
    StyleSheet,
    ScrollView,
    Image,
    TouchableOpacity, ToastAndroid,
} from 'react-native';
import * as ImagePicker from 'react-native-image-picker';
import Icon from 'react-native-vector-icons/FontAwesome';
import {deptInfoGet, deptInfoUpdate, getUserInfoByLoginName} from "../../../util/api";
import {useNavigation} from "@react-navigation/native";

const CompanyEditScreen = (props: { navigation: any }) => {
    const navigation = useNavigation();
    console.log(props);
    const [company, setCompany] = useState({});
    const [companyName, setCompanyName] = useState('');
    const [companyContent, setCompanyContent] = useState('');

    useEffect(() => {
        const unsubscribe = navigation.addListener(
            'focus',
            payload => {
                initPage();
            }
        );
        return () => {
            unsubscribe();
        };
    }, [navigation]);

    //初始化
    const initPage = () => {
        // deptInfo
        deptInfoGet().then(res => {
            setCompany(res.data);
            setCompanyName(res.data.companyname);
            setCompanyContent(res.data.companycontent);
        })
    };

    const submit = () => {
        const data = {
            companyid: company.companyid,
            companyname: companyName,
            companycontent: companyContent,
        };
        deptInfoUpdate(data).then((res) => {
            ToastAndroid.show('修改成功', 2000);
        }).catch((err) => {
            ToastAndroid.show('修改失败', 2000);
        }).finally(() => {
            props.navigation.goBack();
        });
    };

    return (
        <ScrollView contentContainerStyle={styles.container}>
            <Text style={styles.label}>支部名称</Text>
            <TextInput
                style={[styles.input]}
                value={companyName}
                onChangeText={setCompanyName}
                multiline
            />
            <Text style={styles.label}>描述内容</Text>
            <TextInput
                style={[styles.contentTxt]}
                value={companyContent}
                onChangeText={setCompanyContent}
                multiline
            />

            <View style={{alignSelf: 'center'}}>
                <View
                    style={{
                        width: 75,
                        marginRight: 5,
                    }}>
                    <Button title="提交" onPress={submit}/>
                </View>
            </View>
        </ScrollView>
    );
};

const styles = StyleSheet.create({
    container: {
        padding: 20,
        height: '100%'
    },
    label: {
        fontSize: 16,
        marginBottom: 5,
    },
    input: {
        borderWidth: 1,
        borderColor: 'gray',
        borderRadius: 5,
        marginBottom: 10,
        padding: 10,
    },
    contentTxt: {
        textAlignVertical: "top",
        textAlign: "left",
        borderWidth: 1,
        borderColor: 'gray',
        borderRadius: 5,
        marginBottom: 10,
        padding: 10,
        height: '50%'
    },
    button: {
        fontSize: 16,
        color: 'blue',
        marginBottom: 10,
    },
    image: {
        width: 100,
        height: 100,
        marginBottom: 10,
        margin: 2,
    },
});

export default CompanyEditScreen;
